package com.sinodata.bsm.cicp.finder.was6;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.CollectorUtil;

public class ServersFinder extends Finder {

    public static final Logger logger = Logger.getLogger(ServersFinder.class);

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        try {
            String port = (String) params.get("SERVERPORT");
            String url = Util.getWasPerfToolUrl(ip, port);
            String respXml = CollectorUtil.getJmxXml(url, ip, Integer.parseInt(port));
            Document doc = DocumentHelper.parseText(respXml);
            Element root = doc.getRootElement();
            List<Element> nodes = root.elements("Node");

            for (Element node : nodes) {
                String nodeName = node.attributeValue("name");
                List<Element> servers = node.elements("Server");
                for (Element server : servers) {
                    String serName = server.attributeValue("name");
                    String[] info = new String[6];
                    list.add(info);
                    info[0] = nodeName + serName + ip;
                    info[1] = serName;
                    info[2] = serName;
                    info[3] = port; //服务器端口
                    info[4] = serName; //服务器名
                    info[5] = nodeName; //节点名
                }
            }
            System.out.println("**********************开始发现WAS Server 4********************************");
            System.out.println(list.size() + " **********************************");
            return list;
        } catch (Exception e) {
            logger.error("failed to find was6 servers", e);
            return new ArrayList<String[]>();
        }
    }
}
